package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.container.page.PageServlet;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckgroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckgroupService.class)
@Transactional
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupDao checkgroupDao;

    @Override//新增检查项
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkgroupDao.add(checkGroup);
        //获取主键返回值
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                //封装成一个对象传递比较方便
                Map<String, Integer> map = new HashMap<>();
                map.put("checkitem_id", checkitemId);
                map.put("checkgroup_id", checkGroupId);
                checkgroupDao.addGroupAndItem(map);
            }
        }
    }

    @Override//分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkgroupDao.findAll(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询单个检查组
    public CheckGroup findById(Integer id){
        return checkgroupDao.findById(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkgroupDao.edit(checkGroup);
        //清理编辑前的关系
        Integer checkGroupId = checkGroup.getId();
        checkgroupDao.deleteAssociation(checkGroupId);

        if (checkitemIds!=null&&checkitemIds.length>0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("checkitem_id", checkitemId);
                map.put("checkgroup_id", checkGroupId);
                checkgroupDao.addGroupAndItem(map);
            }
        }
    }

    //返回需要编辑的检查组关联的检查项的id，用于复选框的回显
    public List<Integer> findCheckitemByCheckGroup(Integer id){
        List<Integer> checkitemIds = checkgroupDao.findCheckitemByCheckGroup(id);
        if(checkitemIds!=null && checkitemIds.size()>0)return checkitemIds;
        return null;
    }

    //删除检查组，先删除检查项和检查组之间的绑定关系，再进行删除
    public void delete(Integer id){
        checkgroupDao.deleteAssociation(id);
        checkgroupDao.deleteCheckgroup(id);
    }

    public List<CheckGroup> findAll(){
        return checkgroupDao.find();
    }
}
